package youcaihuaoa.weetop.com.vm.main

import android.app.Application
import android.text.TextUtils
import androidx.databinding.ObservableField
import api.UserServiceFactory
import com.blankj.utilcode.util.ToastUtils
import com.robot.base.base.vm.GLBaseViewModel
import com.robot.base.common.api.AbstractViewModelSubscriber
import com.robot.base.configs.AppConstants
import com.robot.base.util.LiveDataBus
import me.goldze.mvvmhabit.binding.command.BindingAction
import me.goldze.mvvmhabit.binding.command.BindingCommand
import me.goldze.mvvmhabit.http.BaseResponse

class MessageGroupCreateViewModel(application: Application) : GLBaseViewModel(application) {
    val groupName = ObservableField<String>()
    val confirmCommand = BindingCommand<Any>(BindingAction {
        ryGroupAdd()
    })

    fun ryGroupAdd() {
        if (TextUtils.isEmpty(groupName.get())) {
            ToastUtils.showShort("请输入群组名称")
            return
        }
        showLoadingDialog()
        UserServiceFactory.ryimGroupAdd(
            groupName.get()
        ).subscribe(object : AbstractViewModelSubscriber<BaseResponse<Any>>(this) {
            override fun onNext(t: BaseResponse<Any>) {
                dismissLoadingDialog()
                LiveDataBus.get().with(AppConstants.LiveKey.REFRESH_ADD_GROUP).postValue("1")
                finish()
            }
        })
    }
}